package ltd.itlover.yqblog.blog;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import java.util.ArrayList;
import java.util.List;

public class ViewPager2Adapter extends FragmentStateAdapter {
    private List<Class> fragmentList;

    ViewPager2Adapter(FragmentActivity fragmentActivity){
        super(fragmentActivity);
        if(fragmentList == null){
            fragmentList = new ArrayList<>();
        }
    }

    public void addFragment(Fragment fragment) {
        if (fragmentList != null) {
            fragmentList.add(fragment.getClass());
        }
    }

    @NonNull
    @Override
    public Fragment createFragment(int position) {
        try {
            return (Fragment) fragmentList.get(position).newInstance();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int getItemCount() {
        return fragmentList.size();
    }


}
